/**
 * @file loop_caed.h
 * @author Morris (morris@mnblog.cn)
 * @brief
 * @version 0.0.1
 * @date 2022-01-14
 *
 * @copyright Copyright (c) 2022
 *
 */
#ifndef __BOARD_LOOP_NODE_H__
#define __BOARD_LOOP_NODE_H__

#include "adc.h"
#include "bgr.h"
#include "ddl.h"
#include "gpio.h"
#include "rtc.h"
#include "bsp_control.h"


/**
 * MCU: HC32L130J8TA QFN48
 * FLASH:   64K
 * RAM:     8K
 * GPIO:    40
 * LPTIM:   1
 * TIM:     7
 * LPUART:  2
 * USART:   2
 * I2C:     2
 * SPI:     2
 * ADC:     17ch
 *
 *                               __|__|__|__|__|__|__|__|__|__|__|__|__
 *                              | 48 47 46 45 44 43 42 41 40 39 38 37 |
 *                            --| 01                               36 |--
 *                            --| 02                               35 |--
 *                  OSC32_IN  --| 03                               34 |--
 *                 OSC32_OUT  --| 04                               33 |--
 *                            --| 05                               32 |--
 *                            --| 06                               31 |--
 *                      NRST  --| 07                               30 |--
 *                            --| 08                               29 |--
 *                            --| 09                               28 |--
 *                            --| 10                               27 |--
 *                       KEY  --| 11                               26 |--
 *                            --| 12                               25 |--
 *                            --| 13 14 15 16 17 18 19 20 21 22 23 24 |--
 *                              --|--|--|--|--|--|--|--|--|--|--|--|--
 *                                   |
 *                   AD_VCAP  -------|
 *
 * Pin NUMBER   PIN NAME        PIN FUN         LABELs
 * 03           OSC32_IN
 * 04           OSC32_OUT
 * 07           NRST
 * 11           PA1             GPIO_INPUT      KEY
 * 14           PA4             ADC             AD_VCAP
 * 18           PB0             GPIO_OUTPUT     ALARM
 * 41           PB5             GPIO_OUTPUT     LOAD
 * 42           PB6             IIC             SCL
 * 43           PB7             IIC             SDA
 */

/**
 *          0x00 未知
 *          0x01 感温
 *          0x02 感烟
 *          0x03 感烟 + 感温
 *          0x04 CO
 *          0x05 感温 + CO
 *          0x06 感烟 + CO
 *          0x07 感烟 + 感温 + CO
 *					0x08 感温棒
 *          0x10 未知
 *          0x11 手报
 *          0x12 输入(1入)
 *          0x13 输出(1出)
 *          0x14 输入输出(1入1出)
 *          0x15 声光报警
 *          0x16 消火栓
 */
 
#if TEXT_SHYS
#define DEVICE_TYPE (0x07)
#else 
#define DEVICE_TYPE (0x08)
#endif

#define WDT_EN (1)

#define SWD_CLK_PORT GpioPortA
#define SWD_CLK_PIN  GpioPin14
#define SWD_DIO_PORT GpioPortA
#define SWD_DIO_PIN  GpioPin13

///< XTH
#ifndef SYSTEM_XTH
#define SYSTEM_XTH (4000000u) ///< 4MHz
#endif

#define STK_XTHI_PORT GpioPortD
#define STK_XTHI_PIN  GpioPin0
#define STK_XTHO_PORT GpioPortD
#define STK_XTHO_PIN  GpioPin1

///< XTL
#ifndef SYSTEM_XTL
#define SYSTEM_XTL (32768u) ///< 32768Hz
#endif
#define STK_XTLI_PORT GpioPortC
#define STK_XTLI_PIN  GpioPin14
#define STK_XTLO_PORT GpioPortC
#define STK_XTLO_PIN  GpioPin15

// #define DEBUG_UART (1)

///< I2C0
#define I2C0_PORT        M0P_I2C0
#define I2C0_PERICLK     SysctrlPeripheralI2c0
#define I2C0_SCL_PORT    GpioPortB
#define I2C0_SCL_PIN     GpioPin6
#define I2C0_SCL_GPIO_AF GpioAf1
#define I2C0_SDA_PORT    GpioPortB
#define I2C0_SDA_PIN     GpioPin7
#define I2C0_SDA_GPIO_AF GpioAf1

///< EEPROM I2C
#define EEPROM_I2C_PORT        I2C0_PORT
#define EEPROM_I2C_PERICLK     I2C0_PERICLK
#define EEPROM_I2C_SCL_PORT    I2C0_SCL_PORT
#define EEPROM_I2C_SCL_PIN     I2C0_SCL_PIN
#define EEPROM_I2C_SCL_GPIO_AF I2C0_SCL_GPIO_AF
#define EEPROM_I2C_SDA_PORT    I2C0_SDA_PORT
#define EEPROM_I2C_SDA_PIN     I2C0_SDA_PIN
#define EEPROM_I2C_SDA_GPIO_AF I2C0_SDA_GPIO_AF

///< ALARM LED
#define LOOP_NODE_ALARM_LED 1
//#define RUN_LED_PORT      GpioPortB
//#define RUN_LED_PIN       GpioPin0

///< LOAD
#define LOAD_PORT GpioPortB
#define LOAD_PIN  GpioPin5

///< RX1
#define RX_1_PORT GpioPortB
#define RX_1_PIN  GpioPin8

///< RX2
#define RX_2_PORT GpioPortB
#define RX_2_PIN  GpioPin9

///< TX
#define TX_PORT GpioPortB
#define TX_PIN  GpioPin10

///< FETL
#define FETL_PORT GpioPortB
#define FETL_PIN  GpioPin11

///< FETR
#define FETR_PORT GpioPortB
#define FETR_PIN  GpioPin12

///< DIR_DRV
#define DIR_DRV_PORT GpioPortB
#define DIR_DRV_PIN  GpioPin13

///< DIR_DETL
#define DIR_DETL_PORT GpioPortB
#define DIR_DETL_PIN  GpioPin14

///< DIR_DETR, 删除

///< AD_VCAP
#define AD_VACP_CH4  AdcExInputCH4
#define AD_VCAP_PORT GpioPortA
#define AD_VCAP_PIN  GpioPin4

///< SW_1_IN
#define SW_1_IN_PORT GpioPortA
#define SW_1_IN_PIN  GpioPin1

///< SW_1_PWR
#define SW_1_PWR_PORT GpioPortA
#define SW_1_PWR_PIN  GpioPin9

#endif /* __BOARD_LOOP_NODE_H__ */
